package cn.com.zaprk;

import cn.com.zaprk.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @PackageName: cn.com.zaprk
 * @ClassName: JDBC_02_Update
 * @Description:
 * @author: RZS
 * @date: 2021/10/31  15:28
 */
public class JDBC_02_Update {
    /**
     * 往user表中添加一条数据
     * @param args
     */
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement stat = null;

        // 获取连接
        try {
            conn = JDBCUtils.getConn();
            // 1.编写sql语句
            String sql = "insert into user(name, money) values(?, ?)";

            // 2.获取PreparedStatement对象
            stat = conn.prepareStatement(sql);
            // 3.替换占位符
            stat.setString(1, "王五");
            stat.setDouble(2, 3000.0);

            // 执行预编译sql
            int rows = stat.executeUpdate();
            System.out.println(rows);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭连接
            JDBCUtils.close(null, stat, conn);
        }
    }
}
